# 0=north
# 1=east
# 2=south
# 3=west
# 4=local


def mesh_routing(hub_id_x, hub_id_y, dest_address_x, dest_address_y):
    x_offset = dest_address_x - hub_id_x
    y_offset = dest_address_y - hub_id_y

    if x_offset < 0:
        if hub_id_x == 1 and hub_id_y == 0:
            return 'south'
        elif hub_id_x == 1 and hub_id_y == 15:
            return 'north'
        else:
            return 'west'
    elif x_offset > 0:
        if hub_id_x == 14 and hub_id_y == 0:
            return 'south'
        elif hub_id_x == 14 and hub_id_y == 15:
            return 'north'
        else:
            return 'east'

    elif x_offset == 0 and y_offset < 0:
        return 'north'
    elif x_offset == 0 and y_offset > 0:
        return 'south'
    elif x_offset == 0 and y_offset == 0:
        return 'local'


if __name__ == "__main__":
    topology = [
        ['x1_1', 'x1_2', 'x1_3', 'x1_4', 'x1_5', 'x1_6', 'x1_7', 'x1_8', 'x1_9', 'x1_10', 'x1_11', 'x1_12', 'x1_13',
         'x1_14', 'x1_15', 'x1_16', ],
        ['x2_1', 'x2_2', 'x2_3', 'x2_4', 'x2_5', 'x2_6', 'x2_7', 'x2_8', 'x2_9', 'x2_10', 'x2_11', 'x2_12', 'x2_13',
         'x2_14', 'x2_15', 'x2_16', ],
        ['x3_1', 'x3_2', 'x3_3', 'x3_4', 'x3_5', 'x3_6', 'x3_7', 'x3_8', 'x3_9', 'x3_10', 'x3_11', 'x3_12', 'x3_13',
         'x3_14', 'x3_15', 'x3_16', ],
        ['x4_1', 'x4_2', 'x4_3', 'x4_4', 'x4_5', 'x4_6', 'x4_7', 'x4_8', 'x4_9', 'x4_10', 'x4_11', 'x4_12', 'x4_13',
         'x4_14', 'x4_15', 'x4_16', ],
        ['x5_1', 'x5_2', 'x5_3', 'x5_4', 'x5_5', 'x5_6', 'x5_7', 'x5_8', 'x5_9', 'x5_10', 'x5_11', 'x5_12', 'x5_13',
         'x5_14', 'x5_15', 'x5_16', ],
        ['x6_1', 'x6_2', 'x6_3', 'x6_4', 'x6_5', 'x6_6', 'x6_7', 'x6_8', 'x6_9', 'x6_10', 'x6_11', 'x6_12', 'x6_13',
         'x6_14', 'x6_15', 'x6_16', ],
        ['x7_1', 'x7_2', 'x7_3', 'x7_4', 'x7_5', 'x7_6', 'x7_7', 'x7_8', 'x7_9', 'x7_10', 'x7_11', 'x7_12', 'x7_13',
         'x7_14', 'x7_15', 'x7_16', ],
        ['x8_1', 'x8_2', 'x8_3', 'x8_4', 'x8_5', 'x8_6', 'x8_7', 'x8_8', 'x8_9', 'x8_10', 'x8_11', 'x8_12', 'x8_13',
         'x8_14', 'x8_15', 'x8_16', ],
        ['x9_1', 'x9_2', 'x9_3', 'x9_4', 'x9_5', 'x9_6', 'x9_7', 'x9_8', 'x9_9', 'x9_10', 'x9_11', 'x9_12', 'x9_13',
         'x9_14', 'x9_15', 'x9_16', ],
        ['x10_1', 'x10_2', 'x10_3', 'x10_4', 'x10_5', 'x10_6', 'x10_7', 'x10_8', 'x10_9', 'x10_10', 'x10_11', 'x10_12',
         'x10_13', 'x10_14', 'x10_15', 'x10_16', ],
        ['x11_1', 'x11_2', 'x11_3', 'x11_4', 'x11_5', 'x11_6', 'x11_7', 'x11_8', 'x11_9', 'x11_10', 'x11_11', 'x11_12',
         'x11_13', 'x11_14', 'x11_15', 'x11_16', ],
        ['x12_1', 'x12_2', 'x12_3', 'x12_4', 'x12_5', 'x12_6', 'x12_7', 'x12_8', 'x12_9', 'x12_10', 'x12_11', 'x12_12',
         'x12_13', 'x12_14', 'x12_15', 'x12_16', ],
        ['x13_1', 'x13_2', 'x13_3', 'x13_4', 'x13_5', 'x13_6', 'x13_7', 'x13_8', 'x13_9', 'x13_10', 'x13_11', 'x13_12',
         'x13_13', 'x13_14', 'x13_15', 'x13_16', ],
        ['x14_1', 'x14_2', 'x14_3', 'x14_4', 'x14_5', 'x14_6', 'x14_7', 'x14_8', 'x14_9', 'x14_10', 'x14_11', 'x14_12',
         'x14_13', 'x14_14', 'x14_15', 'x14_16', ],
        ['x15_1', 'x15_2', 'x15_3', 'x15_4', 'x15_5', 'x15_6', 'x15_7', 'x15_8', 'x15_9', 'x15_10', 'x15_11', 'x15_12',
         'x15_13', 'x15_14', 'x15_15', 'x15_16', ],
        ['x16_1', 'x16_2', 'x16_3', 'x16_4', 'x16_5', 'x16_6', 'x16_7', 'x16_8', 'x16_9', 'x16_10', 'x16_11', 'x16_12',
         'x16_13', 'x16_14', 'x16_15', 'x16_16', ],
    ]

    boundary1 = [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [1, 11], [1, 12], [1, 13],
                 [1, 14], [1, 15], ]
    boundary2 = [[16, 2], [16, 3], [16, 4], [16, 5], [16, 6], [16, 7], [16, 8], [16, 9], [16, 10], [16, 11], [16, 12],
                 [16, 13], [16, 14], [16, 15], ]
    boundary3 = [[2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 1], [8, 1], [9, 1], [10, 1], [11, 1], [12, 1], [13, 1],
                 [14, 1], [15, 1], ]
    boundary4 = [[2, 16], [3, 16], [4, 16], [5, 16], [6, 16], [7, 16], [8, 16], [9, 16], [10, 16], [11, 16], [12, 16],
                 [13, 16], [14, 16], [15, 16], ]

    boundary = boundary1 + boundary2 + boundary3 + boundary4
    print(boundary)
    print()

    for src in boundary:
        for dst in boundary:
            src_id_x = src[0]
            src_id_y = src[1]
            dst_id_x = dst[0]
            dst_id_y = dst[1]
            # print("from ", src_id_x, src_id_y, "to ", dst_id_x, dst_id_x)
            # 初始化hub_id
            nxt_hub_id_x = src_id_x
            nxt_hub_id_y = src_id_y
            print('{\"X', src_id_x, "_", src_id_y, '\",', sep='', end='')
            # print('{\"X', nxt_hub_id_x, '_', nxt_hub_id_y, '\",', sep='', end='')
            while src_id_x != dst_id_x or src_id_y != dst_id_y:
                temp = mesh_routing(nxt_hub_id_x, nxt_hub_id_y, dst_id_x, dst_id_y)
                if temp == 'south':
                    nxt_hub_id_y = nxt_hub_id_y + 1
                    print('\"X', nxt_hub_id_x, '_', nxt_hub_id_y, '\",', sep='', end='')
                elif temp == 'north':
                    nxt_hub_id_y = nxt_hub_id_y - 1
                    print('\"X', nxt_hub_id_x, '_', nxt_hub_id_y, '\",', sep='', end='')
                elif temp == 'west':
                    nxt_hub_id_x = nxt_hub_id_x - 1
                    print('\"X', nxt_hub_id_x, '_', nxt_hub_id_y, '\",', sep='', end='')
                elif temp == 'east':
                    nxt_hub_id_x = nxt_hub_id_x + 1
                    print('\"X', nxt_hub_id_x, '_', nxt_hub_id_y, '\",', sep='', end='')
                else:
                    print("},", end='')
                    break
            print()
        print()
'''
for i in range(16):
    print('[', end='', sep='')
    for j in range(16):
        print('\'', 'x', i+1, '_', j+1, '\'', ',', end='', sep='')
    print('],')'''
print()
print()
for i in range(16):
    print('[', i, ', ', '16], ', sep='', end='')
print()
print()
for i in range(2, 16):
    print("Mesh_switch.A", i, "_17.die_delay = 3.125e-12", sep='')
